#!/usr/bin/env python
# -*- coding: utf-8 -*-
import math

from gm.api import stk_get_daily_valuation, stk_get_daily_valuation_pt

from web.api.base_api import BaseApi
from web.constants.python_api_result import PythonApiResult


class StockPERatioAndPBRatioApi(BaseApi):
    """
    市盈率和市净率API
    """

    def write_pe_and_pb_and_ps_and_peg_and_dy_between_date(self, begin_date: str, end_date: str):
        """
        根据开始时间和结束时间，更新市盈率、市净率、市销率、市盈率相对盈利增长比率和股息率
        """

        stock_info_list = self.stock_info_service.find_all()
        if stock_info_list is not None and len(stock_info_list) > 0:
            for stock_info in stock_info_list:
                _code = None
                if stock_info.url_param[0:2].upper() == 'SH':
                    _code = 'SHSE.' + stock_info.code_
                if stock_info.url_param[0:2].upper() == 'SZ':
                    _code = 'SZSE.' + stock_info.code_

                result_df = stk_get_daily_valuation(symbol=_code, fields='pe_ttm,pb_lyr,ps_ttm,peg_lyr,dy_ttm',
                                         start_date=begin_date, end_date=end_date, df=True)

                result_list = result_df.values.tolist()
                if result_list is not None and len(result_list):
                    for row in result_list:
                        date = row[1]
                        pe = None if math.isnan(row[2]) else row[2]
                        pb = None if math.isnan(row[3]) else row[3]
                        ps = None if math.isnan(row[4]) else row[4]
                        peg = None if math.isnan(row[5]) else row[5]
                        dy = None if math.isnan(row[6]) else row[6]

                        self.stock_transaction_data_all_service.update_pe_and_pb_and_ps_and_peg_and_dy_by_code_and_date(
                            stock_info.code_, date, pe, pb, ps, peg, dy)

        print(PythonApiResult.FINISH)

    def write_pe_and_pb_and_ps_and_peg_and_dy_by_date(self, date: str):
        """
        根据日期，更新市盈率、市净率、市销率、市盈率相对盈利增长比率和股息率
        """

        stock_info_list = self.stock_info_service.find_all()
        if stock_info_list is not None and len(stock_info_list) > 0:
            param_code_list = list()
            for stock_info in stock_info_list:
                _code = None
                if stock_info.url_param[0:2].upper() == 'SH':
                    _code = 'SHSE.' + stock_info.code_
                if stock_info.url_param[0:2].upper() == 'SZ':
                    _code = 'SZSE.' + stock_info.code_
                param_code_list.append(_code)

            result_df = stk_get_daily_valuation_pt(symbols=param_code_list, fields='pe_ttm,pb_lyr,ps_ttm,peg_lyr,dy_ttm',
                                   trade_date=date, df=True)
            result_list = result_df.values.tolist()
            if result_list is not None and len(result_list):
                for row in result_list:
                    code = row[0].split(".")[1]
                    date = row[1]
                    pe = None if math.isnan(row[2]) else row[2]
                    pb = None if math.isnan(row[3]) else row[3]
                    ps = None if math.isnan(row[4]) else row[4]
                    peg = None if math.isnan(row[5]) else row[5]
                    dy = None if math.isnan(row[6]) else row[6]

                    self.stock_transaction_data_all_service.update_pe_and_pb_and_ps_and_peg_and_dy_by_code_and_date(
                        code, date, pe, pb, ps, peg, dy)

        print(PythonApiResult.FINISH)